<?php

namespace app\store\controller;

use library\Controller;
use think\Db;

/**
 * 基础数据
 * Class GoodsCate
 * @package app\store\controller
 */
class Base extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    public static $CHANNEL = 888;//平台渠道
    protected $table_DB_Platform_Account_log_library = 'db_platform_log';       /*平台日志库*/
    protected $table_DB_Platform_Account_library = 'db_platform_accounts';      /*平台账户配置库*/
    protected $table_DB_Platform_Account_statistics = 'db_platform_statistics'; /*平台账户配置库*/
    protected $day_user_online = 'day_user_online';                             /* 在线用户统计 */
    protected $accounts_info = 'accounts_info';                                 /* 用户表 */
    protected $user_tradelog = 'tradelog';                                      /* 订单记录表 */
    protected $user_stat_remain = 'stat_remain';                                      /* 留存记录表 */
    protected $user_recharge_info = 'recharge_info';                            /* 充值情况记录表 */
    protected $user_data_info = 'data_info';                                    /* 日活记录表 */
    protected function Db_connect_day_user_online()
    {
        $day_user_online = Db::connect($this->table_DB_Platform_Account_log_library)->table($this->day_user_online);
        return $day_user_online;
    }

    protected function Db_connect_accounts_info()
    {
        $accounts_info = Db::connect($this->table_DB_Platform_Account_library)->table($this->accounts_info);
        return $accounts_info;
    }

    protected function Db_connect_user_data_info()
    {
        $user_data_info = Db::connect($this->table_DB_Platform_Account_statistics)->table($this->user_data_info);
        return $user_data_info;
    }

    protected function Db_connect_user_recharge_info()
    {
        $user_recharge_info = Db::connect($this->table_DB_Platform_Account_statistics)->table($this->user_recharge_info);
        return $user_recharge_info;
    }

    protected function Db_connect_user_tradelog()
    {
        $user_tradelog = Db::connect($this->table_DB_Platform_Account_library)->table($this->user_tradelog);
        return $user_tradelog;
    }

    protected function Db_connect_user_stat_remain()
    {
        $user_stat_remain = Db::connect($this->table_DB_Platform_Account_library)->table($this->user_stat_remain);
        return $user_stat_remain;
    }

    /**
     * 商品分类管理
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '基础数据';
        $this->assign('user_data_info', $this->user_data_info());
        $this->assign('user_thirty_day_remain', $this->statRemain());
        $this->fetch();
    }

    // 留存
    public function statRemain()
    {
        // 最近30日留存情况
        $arr = $this->Db_connect_user_stat_remain()
            ->field('second_day,third_day,seventh_day,fifth_day,stat_time')
            ->where('channel','=',self::$CHANNEL)
            ->order('stat_time desc')
            ->limit(30)
            ->select();
        $this->totalJson = [];
        foreach ($arr as $value) {
            $this->totalJson['second_day_count'][] = round(sprintf("%.4f",$value['second_day']*100),2);
            $this->totalJson['third_day_count'][] = round(sprintf("%.4f",$value['third_day']*100),2);
            $this->totalJson['seventh_day_count'][] = round(sprintf("%.4f",$value['seventh_day']*100),2);
            $this->totalJson['fifth_day_count'][] = round(sprintf("%.4f",$value['fifth_day']*100),2);
            $time = explode(' ',$value['stat_time']);
            $this->totalJson['time'][] = $time[0];
        }
        $this->totalJson['second_day_count'] = array_reverse($this->totalJson['second_day_count']);
        $this->totalJson['third_day_count'] = array_reverse($this->totalJson['third_day_count']);
        $this->totalJson['seventh_day_count'] = array_reverse($this->totalJson['seventh_day_count']);
        $this->totalJson['fifth_day_count'] = array_reverse($this->totalJson['fifth_day_count']);
        $this->totalJson['time'] = array_reverse($this->totalJson['time']);
        return $this->totalJson;
    }

    /* 新增玩家 活跃玩家 玩家充值 三种渠道分布数据 */
    public function user_data_info()
    {
        $time = date('Y-m-d', strtotime('-30 day'));
        $where[] = ['time', '>=', $time];
        $arr = $this->Db_connect_user_data_info()->fieldRaw('sum(newcount) as mynewcount,sum(logincount) as mylogincount,sum(recharge) as myrecharge,time,channel_id')->where($where)->group('channel_id')->select();
        $this->totalJson = [];
        $arr1 = $arr2 = $arr3 = [];
        foreach ($arr as $key=>$value) {
            $arr1[$key]['count'] = $value['mynewcount'];
            $arr2[$key]['count'] = $value['mylogincount'];
            $arr3[$key]['count'] = $value['myrecharge'];
            if ($value['channel_id'] == 0) {
                $arr1[$key]['channel_name'] = $arr2[$key]['channel_name'] = $arr3[$key]['channel_name'] = '官方-0-渠道';

            } else {
                $arr1[$key]['channel_name'] = $arr2[$key]['channel_name'] = $arr3[$key]['channel_name'] = get_channel_name($value['channel_id']);
            }
        }
        // 对数组按照count进行排序
        array_multisort(array_column($arr1, 'count'), SORT_DESC,$arr1);
        array_multisort(array_column($arr2, 'count'), SORT_DESC,$arr2);
        array_multisort(array_column($arr3, 'count'), SORT_DESC,$arr3);
        // 组合一维数组
        foreach ($arr1 as $value){
            if($value['count']>0){
                $this->totalJson['mynewcount']['data'][] = $value['count'];
                $this->totalJson['mynewcount']['channel_name'][] = $value['channel_name'];
            }
        }
        foreach ($arr2 as $value){
            if($value['count']>0){
                $this->totalJson['mylogincount']['data'][] = $value['count'];
                $this->totalJson['mylogincount']['channel_name'][] = $value['channel_name'];
            }
        }
        foreach ($arr3 as $value){
            if($value['count']>0){
                $this->totalJson['myrecharge']['data'][] = $value['count'];
                $this->totalJson['myrecharge']['channel_name'][] = $value['channel_name'];
            }
        }
        return $this->totalJson;
    }

    /* 渠道充值分布情况 */
    public function user_recharge_info()
    {
        $time = date('Y-m-d', time());
        $where[] = ['time', 'EQ', $time];
        $arr = $this->Db_connect_user_recharge_info()->field('spreaderId,recharge')->where($where)->select();
        $this->totalJson = ['spreaderId' => [], 'recharge' => []];
        foreach ($arr as $value) {
            $this->totalJson['recharge'][] = $value['recharge'];
            if ($value['spreaderId'] == 0) {
                $channel_name = '官方-0-渠道';
            } else {
                $channel_name = get_channel_name($value['spreaderId']);
            }
            $this->totalJson['spreaderId'][] = $channel_name;
        }
        return $this->totalJson;
    }
    protected function structure_where()
    {
        $channel_type = $this->request->param('channel_type');
        $search_time = $this->request->param('search_time');
        $where = [];
        if (!empty($search_time)) {
            $list = explode(' - ', $search_time);
            $begin = $list[0];
            $after = $list[1];
            $where[] = ['stat_time', 'between', [$begin, $after]];
        } else {
            $begin = date('Y-m-d', time() - 7 * 24 * 3600);
            $after = date('Y-m-d', time() - 24 * 3600);
            $where[] = ['stat_time', 'between', [$begin, $after]];
        }
        if (!empty($channel_type)) {
            if ($channel_type == 30) $channel_type = 0;
            $where[] = ['channel', 'eq', $channel_type];
        } else {
            $where[] = ['channel', 'eq', 888];
        }
        return $where;
    }

    /**
     * 添加商品分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        $this->title = '添加商品分类';
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑商品分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->title = '编辑商品分类';
        $this->_form($this->table, 'form');
    }

    /**
     * 禁用商品分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        $this->_save($this->table, ['status' => '0']);
    }

    /**
     * 启用商品分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        $this->_save($this->table, ['status' => '1']);
    }

    /**
     * 删除商品分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        $this->_delete($this->table);
    }

}
